package com.softer.chengxin.traning.controller;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.traning.entity.TraningDetail;
import com.softer.chengxin.traning.service.TraningDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@RestController
@RequestMapping("/api/traningDetail")
public class TraningDetailController {
    @Autowired
    private TraningDetailService traningDetailService;

    @PostMapping("/add")
    public CommonResult addTraningDetail(TraningDetail traningDetail) {
        return traningDetailService.addTraning(traningDetail);
    }

    @PostMapping("/getAllListByTranId")
    public List<TraningDetail> getAllListByTranId(String id, String state) {

        return traningDetailService.getAllListByTranId(id, state);
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @PostMapping("/getInfo")
    public TraningDetail getInfo(String id) {
        return traningDetailService.getInfo(id);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param state
     * @return
     */

    @PostMapping("/changeState")
    public CommonResult changeState(String id, String state, String recRemark, HttpSession session) {
        String name = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isEmpty(name)) {
            return CommonResult.response(NumberEnum.ZERO.getNum(), "请重新登陆后操作");
        }
        return traningDetailService.changeState(id, state, recRemark, name);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/del")
    public CommonResult del(String id) {
        return traningDetailService.del(id);
    }

    /**
     * 修改
     *
     * @param traningDetail
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(TraningDetail traningDetail) {
        return traningDetailService.update(traningDetail);
    }

    /**
     * @param id
     * @param state
     * @param session
     * @return
     */
    @PostMapping("/getAllListByTranIdAndDep")
    public List<TraningDetail> getAllListByTranIdAndDep(String id, String state, HttpSession session) {
        //获取该登录账号的管理级别
        String name = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isEmpty(name)) {
            return new ArrayList<>();
        }
        return traningDetailService.getAllListByTranIdAndDep(id, state, name);
    }

    /**
     * 查询培训人员的培训记录
     * 用于培训查看页面
     *
     * @param id traning_info ID
     * @return 培训记录
     */
    @PostMapping("/getTrainingDetailList")
    public List<TraningDetail> getTrainingDetailList(int id) {
        return traningDetailService.getTrainingDetailList(id);
    }
}